using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace PBR.Controls
{
    public partial class MaterialsPanel : UserControl
    {
        #region Member Variables

        private Scene scene;

        #endregion

        #region Member Functions

        public MaterialsPanel(Scene scene)
        {
            InitializeComponent();

            this.scene = scene;

            // add the default material
            ListViewItem defaultMatLV = new ListViewItem(scene.Painting.defaultMaterial.Name, 0);
            defaultMatLV.Tag = scene.Painting.defaultMaterial;
            materialsListView.Items.Add(defaultMatLV);
        }

        private void materialsListView_DragEnter(object sender, DragEventArgs e)
        {

        }

        private void materialsListView_DragLeave(object sender, EventArgs e)
        {
            
        }

        private void materialsListView_DragDrop(object sender, DragEventArgs e)
        {
        }

        private void materialsListView_ItemDrag(object sender, ItemDragEventArgs e)
        {
            if (materialsListView.SelectedItems.Count < 1)
            {
                return;
            }
            Material mat = (Material)materialsListView.SelectedItems[0].Tag;
            DoDragDrop(mat.Name, DragDropEffects.Copy);
        }

        private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (materialsListView.SelectedItems.Count < 1)
            {
                return;
            }
            materialsListView.SelectedItems[0].BeginEdit();
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        #endregion

        #region ToolStrip Functions/Events

        /// <summary>
        /// Create a new material and add it to the materialListView control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addMaterialToolStripButton_Click(object sender, EventArgs e)
        {
            string matName = "New Material" + materialsListView.Items.Count.ToString();

            Material newMat = new Material(matName, scene.Painting.defaultMaterial);
            scene.Painting.MaterialList.Add(newMat);

            ListViewItem newMatLV = new ListViewItem(matName, 0);
            newMatLV.Tag = newMat;
            materialsListView.Items.Add(newMatLV);
            newMatLV.BeginEdit();
        }

        private void removeMaterialToolStripButton_Click(object sender, EventArgs e)
        {
            // need at least one element to delete
            if(materialsListView.SelectedItems.Count < 1)
            {
                return;
            }

            // can't delete the default material
            if ((materialsListView.SelectedItems[0]).Name == scene.Painting.defaultMaterial.Name)
            {
                return;
            }

            foreach (ListViewItem lvItem in materialsListView.SelectedItems)
            {
                lvItem.Remove();
            }
        }

        #endregion

        private void materialsListView_AfterLabelEdit(object sender, LabelEditEventArgs e)
        {
            ListViewItem editedMatLVItem = materialsListView.Items[e.Item];
            Material editedMat = (Material)editedMatLVItem.Tag;
            if (e.Label == null)
            {
                return;
            }

            foreach (ListViewItem lvItem in materialsListView.Items)
            {
                if (e.Label == lvItem.Text)
                {
                    editedMatLVItem.BeginEdit();
                }
            }
            editedMat.Name = e.Label;

            editedMatLVItem.Name = e.Label;

            materialsListView.Sort();
            materialsListView.Refresh();
        }

        private void materialsListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (materialsListView.SelectedItems.Count < 1)
            {
                return;
            }
            scene.SelectedMat = (Material)materialsListView.SelectedItems[0].Tag;
            PBR.Controls.SegmentPropertiesControl segmentPropertiesControl = (PBR.Controls.SegmentPropertiesControl)scene.MainForm.propertiesTab.Controls[0];
            segmentPropertiesControl.InitSegment();
            scene.MainForm.materialProperties.InitSegment();
            segmentPropertiesControl.Invalidate();
        }



    }
}
